package com.oa.modules.approvalsec1.controller;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;

import com.oa.modules.approvalsec1.entity.AdministrativeMattersEntity;
import com.oa.modules.approvalsec1.service.AdministrativeMattersService;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;


/**
 * @author Hankaicheng
 * @email 3131408121@qq.com
 * @date 2022-08-29 10:09:16
 */
@RestController
@RequestMapping("/administrativematters")
@Api(tags = "相关接口")

public class AdministrativeMattersController extends AbstractController {
    @Autowired
    private AdministrativeMattersService administrativeMattersService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = administrativeMattersService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") Integer id) {
        AdministrativeMattersEntity administrativeMatters = administrativeMattersService.getById(id);
        return R.ok().put("administrativeMatters", administrativeMatters);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody List<AdministrativeMattersEntity> matters) {
        matters.forEach(item -> {
            List<AdministrativeMattersEntity> administrativeMattersEntityList = administrativeMattersService.list(new QueryWrapper<AdministrativeMattersEntity>().eq("code", item.getCode()));
            List<AdministrativeMattersEntity> mattersEntitys = administrativeMattersEntityList.stream().filter(e -> e.getState()!=null&&e.getState() != 4).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(administrativeMattersEntityList)){
                List<AdministrativeMattersEntity> administrativeMatters = administrativeMattersEntityList.stream().filter(e -> e.getState()!=null&&e.getState() == 4).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(administrativeMatters)&& item.getState()!=null&&item.getState()==4 ){
                    item.setId(administrativeMatters.get(0).getId());
                    item.setUpdateDate(new Date());
                    administrativeMattersService.updateById(item);
                }else if(CollectionUtils.isEmpty(administrativeMatters)&&item.getState()!=null &&item.getState()==4){
                    item.setCreateDate(new Date());
                    administrativeMattersService.save(item);
                }else if(!CollectionUtils.isEmpty(mattersEntitys)){
                    item.setId(mattersEntitys.get(0).getId());
                    item.setUpdateDate(new Date());
                    administrativeMattersService.updateById(item);
                }
            }
            else{
                item.setCreateDate(new Date());
                administrativeMattersService.save(item);
            }
        });
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody AdministrativeMattersEntity administrativeMatters) {
        administrativeMattersService.updateById(administrativeMatters);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody Integer[] ids) {
        administrativeMattersService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 政务事项统计
     *
     * @return
     */
    @GetMapping("/zwwtj")
    public R zwwtj() {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("stateTj", administrativeMattersService.matterStateTj());
        map.put("dateTj", administrativeMattersService.matterDateTj());
        return R.ok().put("data", map);
    }

    /**
     * 获取过去七天在一个月内的日期（日）
     *
     * @return
     */
    @GetMapping("/getWeekDate")
    public R getWeekDate() {
        ArrayList<Object> result = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            HashMap<Object, Object> day = new HashMap<>();
            day.put("id", i);
            day.put("name", DateUtil.dayOfMonth(DateUtil.offsetDay(new Date(), -i)));
            result.add(day);
        }
        return R.ok().put("data", result);
    }

    @GetMapping("/zwtj")
    public R zwtj() {
        List<HashMap> zwtj = administrativeMattersService.zwtj();
        return R.ok().put("data", zwtj);
    }


}
